import { ElMessage } from "element-plus";

export const formatNumber = (param) => {
  let data = param;
  if (!data && data != 0) {
    return "";
  } else if (data == 'OFF' || data == 'ON') {
    return data;
  } else {
    // return  Number(parseFloat(data).toFixed(2)).toLocaleString()
    const floatData = parseFloat(data)
    return floatData ? Number(floatData.toFixed(2)).toLocaleString() : data
  }
};

export const getNextCode = (code) => {
  const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  const index = alphabet.indexOf(code);
  const nextIndex = (index + 1) % alphabet.length;
  return alphabet[nextIndex];
};

export const getCurrentDate = () => {
  const now = new Date();
  const year = now.getFullYear();
  const month = String(now.getMonth() + 1).padStart(2, "0");
  const day = String(now.getDate()).padStart(2, "0");
  return `(${month}/${day}/${year})`;
};

export const getParameterFromUrl = (paramName) => {
  const urlParams = new URLSearchParams(window.location.search);
  return urlParams.get(paramName);
};

export const getMachineCode = () => {
  const userAgent = navigator.userAgent;
  const language = navigator.language;
  const screenWidth = window.screen.width;
  const screenHeight = window.screen.height;
  const colorDepth = window.screen.colorDepth;

  const machineCode = `${userAgent}${language}${screenWidth}${screenHeight}${colorDepth}`;
  return machineCode;
};

export const exportFile = (fileResult, fileName = "模板.xlsx") => {
  if (
    fileName.indexOf(".xls") == -1 &&
    fileName.indexOf(".jpg") == -1 &&
    fileName.indexOf(".xlsx") == -1 &&
    fileName.indexOf(".zip") == -1 &&
    fileName.indexOf(".pptx") == -1 &&
    fileName.indexOf(".png") == -1 &&
    fileName.indexOf(".pdf") == -1 &&
    fileName.indexOf(".docx") == -1 &&
    fileName.indexOf(".txt") == -1 &&
    fileName.indexOf(".csv") == -1
  ) {
    fileName = fileName + ".xlsx";
  }
  const blob = new Blob([fileResult], {
    type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
  });
  if ("download" in document.createElement("a")) {
    // 非IE下载
    const elink = document.createElement("a");

    elink.download = fileName;
    elink.style.display = "none";
    console.log(elink, 111);
    elink.href = URL.createObjectURL(blob);

    console.log(elink);
    document.body.appendChild(elink);
    elink.click();
    URL.revokeObjectURL(elink.href); // 释放URL 对象
    document.body.removeChild(elink);
  } else {
    // IE10+下载
    navigator.msSaveBlob(blob, fileName);
  }
  ElMessage.success('Export Successful');
};
export function lowerBounding(arr, target) {
  let l = 0, r = arr.length - 1
  while (l <= r) {
    const mid = l + Math.ceil((r - l) / 2)
    if (arr[mid] < target) {
      l = mid + 1
    } else {
      r = mid - 1
    }
  }
  return l
}


// 获取当前卡尔加里的时间
export function convertBeijingToCalgary() {
  const options = {
    timeZone: 'America/Edmonton',
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit',
    hour12: false
  };

  // 直接生成目标格式的字符串
  const formatter = new Intl.DateTimeFormat('en-CA', options);
  const calgaryTime = formatter.format(new Date())
    .replace(/(\d{4})-(\d{2})-(\d{2}),? (\d{2}:\d{2}:\d{2})/, '$1-$2-$3 $4');

  // 1. 将日期字符串转换为 Date 对象
  const date = new Date(calgaryTime);

  // 2. 获取时间戳（毫秒）
  const timestampMs = date.getTime();
  return timestampMs

}
export function  deepClone(obj) {
  if (typeof obj !== 'object' || obj === null) return obj;

  const copy = Array.isArray(obj) ? [] : {};

  for (let key in obj) {
    if (obj.hasOwnProperty(key)) {
      copy[key] = deepClone(obj[key]);
    }
  }

  return copy;
}